package org.dizem.minichat.view.client;

import craky.component.JImagePane;
import craky.componentc.*;
import craky.layout.LineLayout;
import org.dizem.minichat.Constants;
import org.dizem.minichat.model.User;
import org.dizem.minichat.util.ImageUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: DIZEM
 * Time: 11-3-26 上午9:37
 */
public class LoginFrame extends JFrame {

	private JComponent content;
	private JPanel extendPanel;
	private JImagePane bannerPane;
	private JCButton btnLogin;
	private JCButton btnSetting;
	private JCComboBox txtAccount = new JCComboBox();
	private JCLimitedPassword txtPassword;
	private JCCheckBox cbSavePassword = new JCCheckBox(Constants.STR_SAVE_PASSWORD);
	private JCCheckBox cbAutoLogin = new JCCheckBox(Constants.STR_AUTO_LOGIN);
	private JCLabel lblRegister = new JCLabel(Constants.STR_REGISTER);
	private JCLabel lblGetBack = new JCLabel(Constants.STR_GET_BACK);
	private JCTextField txtServerIP;
	private JCTextField txtServerPort;
	private JCLabel txtServerClear;
	private boolean showExtentPanel = false;

	public LoginFrame() {
		initUI();
		initBanner();
		initHistory();
		setVisible(true);
	}

	private void initHistory() {

	}

	private void initUI() {
		content = new JPanel();
		setLayout(new BorderLayout());
		add(content, BorderLayout.CENTER);
		initExtendPanel();
		add(extendPanel, BorderLayout.SOUTH);


		content.setPreferredSize(Constants.DEFAULT_LOGIN_WINDOW_SIZE);
		content.setLayout(new BorderLayout());
		content.setBorder(new EmptyBorder(0, 0, 0, 0));
		content.setBackground(Color.WHITE);

		setIconImage(ImageUtil.getImage("logo.png"));
		setTitle(Constants.APP_TITLE);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		initContent();
		pack();

		getRootPane().setDefaultButton(btnLogin);
		setLocationRelativeTo(null);
	}

	private void initExtendPanel() {
		extendPanel = new JPanel();
		extendPanel.setVisible(false);
		extendPanel.setPreferredSize(new Dimension(69, 80));
		extendPanel.setBackground(new Color(228, 244, 255));
		extendPanel.setBorder(new GridBorder(Color.white, 0, 1, 0, 1));
		extendPanel.setLayout(new LineLayout(0, 0, 0, 0, 0,
				LineLayout.LEADING, LineLayout.LEADING, LineLayout.VERTICAL));
		JCLabel lblServerIP = new JCLabel("服务器地址");
		lblServerIP.setBorder(new EmptyBorder(1, 0, 0, 10));
		lblServerIP.setPreferredSize(new Dimension(74, 24));
		lblServerIP.setHorizontalAlignment(JCLabel.RIGHT);

		JCLabel lblServerPort = new JCLabel("端口");
		lblServerPort.setBorder(new EmptyBorder(1, 0, 0, 10));
		lblServerPort.setPreferredSize(new Dimension(74, 24));
		lblServerPort.setHorizontalAlignment(JCLabel.RIGHT);

		txtServerIP = new JCTextField("127.0.0.1");
		txtServerIP.setPreferredSize(new Dimension(30,  26));
		txtServerPort = new JCTextField("82");
		txtServerPort.setPreferredSize(txtServerIP.getPreferredSize());

		txtServerClear = new JCLabel(Constants.STR_CLEAR);
		txtServerClear.setBorder(new EmptyBorder(1, 10, 0, 0));
		txtServerClear.setPreferredSize(new Dimension(75, 24));
		txtServerClear.setForeground(new Color(22, 112, 235));
		txtServerClear.setCursor(new Cursor(Cursor.HAND_CURSOR));

		EmptyComponent ecServerIP = new EmptyComponent();
		ecServerIP.setLayout(new LineLayout(0, 0, 0, 14, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecServerIP.setPreferredSize(new Dimension(-1, 40));
		ecServerIP.add(lblServerIP, LineLayout.START);
		ecServerIP.add(txtServerIP, LineLayout.MIDDLE);
		ecServerIP.add(txtServerClear, LineLayout.END);


		EmptyComponent ecServerPort = new EmptyComponent();
		ecServerPort.setLayout(new LineLayout(0, 0, 0, 5, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecServerPort.setPreferredSize(new Dimension(-1, 30));
		ecServerPort.add(lblServerPort, LineLayout.START);
		ecServerPort.add(txtServerPort, LineLayout.START);

		extendPanel.add(ecServerIP, LineLayout.START_FILL);
		extendPanel.add(ecServerPort, LineLayout.START_FILL);
	}

	private void initContent() {
		ActionHandler actionListener = new ActionHandler();

		btnLogin = new JCButton(Constants.STR_LOGIN);
		btnLogin.setPreferredSize(new Dimension(69, 21));
		btnLogin.addActionListener(actionListener);
		btnSetting = new JCButton(Constants.STR_SETTING);
		btnSetting.setPreferredSize(btnLogin.getPreferredSize());
		btnSetting.addActionListener(actionListener);

		bannerPane = new JImagePane();
		JPanel centerPane = new JPanel();
		JImagePane bottomPane = new JImagePane();
		bannerPane.setPreferredSize(new Dimension(334, 64));
		bannerPane.setMode(JImagePane.TILED);
		bottomPane.setPreferredSize(new Dimension(334, 30));
		bottomPane.setImage(ImageUtil.getImage("login_bottom.png"));
		bottomPane.setPreferredSize(new Dimension(334, 30));
		bottomPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		bottomPane.setLayout(new LineLayout(5, 5, 7, 5, 0,
				LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
		bottomPane.add(btnSetting, LineLayout.START);
		bottomPane.add(btnLogin, LineLayout.END);

		centerPane.setBackground(new Color(228, 244, 255));
		centerPane.setBorder(new GridBorder(Color.white, 0, 1, 0, 1));
		centerPane.setLayout(new LineLayout(0, 0, 0, 0, 0,
				LineLayout.LEADING, LineLayout.LEADING, LineLayout.VERTICAL));
		initCenterPane(centerPane);

		content.add(bannerPane, BorderLayout.NORTH);
		content.add(bottomPane, BorderLayout.SOUTH);
		content.add(centerPane, BorderLayout.CENTER);

	}

	private void initBanner() {
		Image image = new ImageIcon(Constants.BANNER_DIR + Constants.FILE_SEP + "5.png").getImage();
		bannerPane.setImage(image);
	}

	private void initCenterPane(JPanel centerPane) {
		JCLabel lblAccount = new JCLabel(Constants.STR_ACCOUNT);
		lblAccount.setBorder(new EmptyBorder(1, 0, 0, 10));
		lblAccount.setPreferredSize(new Dimension(74, 24));
		lblAccount.setHorizontalAlignment(JCLabel.RIGHT);

		JCLabel lblPassword = new JCLabel(Constants.STR_PASSWORD);
		lblPassword.setBorder(new EmptyBorder(1, 0, 0, 10));
		lblPassword.setPreferredSize(new Dimension(74, 24));
		lblPassword.setHorizontalAlignment(JCLabel.RIGHT);

		txtAccount.setToolTipText(Constants.TOOLTIPS_COMBO_ACCOUNT);

		lblRegister.setBorder(new EmptyBorder(1, 10, 0, 0));
		lblRegister.setPreferredSize(new Dimension(75, 24));
		lblRegister.setForeground(new Color(22, 112, 235));
		lblRegister.setCursor(new Cursor(Cursor.HAND_CURSOR));
		lblRegister.setToolTipText(Constants.TOOLTIPS_REGISTER);

		lblGetBack.setBorder(new EmptyBorder(1, 10, 0, 0));
		lblGetBack.setPreferredSize(lblRegister.getPreferredSize());
		lblGetBack.setForeground(lblRegister.getForeground());
		lblGetBack.setCursor(new Cursor(Cursor.HAND_CURSOR));
		lblGetBack.setToolTipText(Constants.TOOLTIPS_GET_BACK);

		txtAccount.setPreferredSize(new Dimension(-1, 26));
		txtAccount.setEditable(true);

		txtPassword = new JCLimitedPassword(Constants.MAX_PASSWORD_LENGTH);


		EmptyComponent ecUser = new EmptyComponent();
		ecUser.setLayout(new LineLayout(0, 0, 0, 14, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecUser.setPreferredSize(new Dimension(-1, 50));
		ecUser.add(lblAccount, LineLayout.START);
		ecUser.add(txtAccount, LineLayout.MIDDLE);
		ecUser.add(lblRegister, LineLayout.END);

		EmptyComponent ecPwd = new EmptyComponent();
		ecPwd.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecPwd.setPreferredSize(new Dimension(-1, 30));
		ecPwd.add(lblPassword, LineLayout.START);
		ecPwd.add(txtPassword, LineLayout.MIDDLE);
		ecPwd.add(lblGetBack, LineLayout.END);

		EmptyComponent ecStatus = new EmptyComponent();
		ecStatus.setLayout(new LineLayout(12, 105, 0, 0, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecStatus.setPreferredSize(new Dimension(-1, 27));
		ecStatus.add(cbSavePassword, LineLayout.START);
		ecStatus.add(cbAutoLogin, LineLayout.START);

		centerPane.add(ecUser, LineLayout.START_FILL);
		centerPane.add(ecPwd, LineLayout.START_FILL);
		centerPane.add(ecStatus, LineLayout.END_FILL);
	}

	private class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();

			if (source == btnLogin) {
			} else if (source == btnSetting) {
				if (!showExtentPanel)
					LoginFrame.this.setSize(getWidth(), getHeight() + 80);
				else
					LoginFrame.this.setSize(getWidth(), getHeight() - 80);
				showExtentPanel = !showExtentPanel;
				extendPanel.setVisible(showExtentPanel);
			}
		}
	}

	public static void main(String[] args) {
		new LoginFrame();
	}

}
